////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010-2011 Kentaro Sekimoto  All rights reserved.
////////////////////////////////////////////////////////////////////////////

//#define TEST_AD
//#define TEST_BUTTON
//#define TEST_EVENTS
//#define TEST_FLASH
//#define TEST_I2C
//#define TEST_LCD
#define TEST_LWIP
//#define TEST_MEMORY
//#define TEST_PWM
//#define TEST_SD
//#define TEST_TOUCHPANEL
//#define TEST_USB

#include <tinyhal.h>
#include "nativesample.h"

#ifdef TEST_AD
#include "TestAD.cpp"
void TestAD(void);
#endif

#ifdef TEST_BUTTON
#include "TestButton.cpp"
void TestButton(void);
#endif

#ifdef TEST_EVENTS
#include "TestEvents.cpp"
void TestEvent(void);
#endif

#ifdef TEST_FLASH
#include "TestFlash.cpp"
void TestFlash(void);
#endif

#ifdef TEST_I2C
#include "TestI2C.cpp"
void TestI2C(void);
#endif

#ifdef TEST_LCD
#include "TestLCD.cpp"
void TestLCD(void);
#endif

#ifdef TEST_LWIP
#include "TestlwIP.cpp"
void TestlwIP(void);
#endif

#ifdef TEST_MEMORY
#include "TestMemory.cpp"
void TestMemory(void);
#endif

#ifdef TEST_PWM
#include "TestPWM.cpp"
void TestPWM(void);
#endif

#ifdef TEST_SD
#include "TestSD.cpp"
void TestSD(void);
#endif

#ifdef TEST_TOUCHPANEL
#include "TestTouchPanel.cpp"
void TestTouchPanel(void);
#endif

#ifdef TEST_USB
#include "TestUSB.cpp"
void TestUSB(void);
#endif

//HAL_DECLARE_NULL_HEAP();
HAL_DECLARE_CUSTOM_HEAP(SimpleHeap_Allocate, SimpleHeap_Release,SimpleHeap_ReAllocate);

#ifndef TEST_TOUCHPANEL
void PostManagedEvent(UINT8 category, UINT8 subCategory, UINT16 data1, UINT32 data2)
{
}
#endif

void wait(volatile UINT64 count)
{
    while (count-- > 0) ;
}

void memory_dump(unsigned long addr, int len)
{
    int i;
    char *p = (char *)addr;
    for (i = 0; i < len; i++) {
        if ((i & 0xf) == 0) {
            debug_printf("%08x ", (unsigned long)p);
        }
        debug_printf("%02x ", (unsigned char)(*p & 0xff));
        if ((i & 0xf) == 0xf) {
            debug_printf("\r\n");
        }
        p++;
    }
    debug_printf("\r\n");
}

#if 0
void JTAG_init(void)
{
    CPU_GPIO_DisablePin(0x00, (GPIO_RESISTOR)0, 0, (GPIO_ALT_MODE)1);
    CPU_GPIO_DisablePin(0x01, (GPIO_RESISTOR)0, 0, (GPIO_ALT_MODE)1);
    CPU_GPIO_DisablePin(0x02, (GPIO_RESISTOR)0, 0, (GPIO_ALT_MODE)1);
    CPU_GPIO_DisablePin(0x03, (GPIO_RESISTOR)0, 0, (GPIO_ALT_MODE)1);
    CPU_GPIO_DisablePin(0x04, (GPIO_RESISTOR)0, 0, (GPIO_ALT_MODE)1);
}
#endif

void ApplicationEntryPoint()
{
    UINT8 *BaseAddress;
    UINT32 SizeInBytes;

    //  JTAG_init();
    HeapLocation(BaseAddress, SizeInBytes);
    SimpleHeap_Initialize(BaseAddress, SizeInBytes);
    HalSystemConfig.DebugTextPort = COM1;
    DebuggerPort_Initialize(HalSystemConfig.DebugTextPort);

#ifdef TEST_MEMORY
    TestMemory();
#endif
#ifdef TEST_AD
    TestAD();
#endif
#ifdef TEST_BUTTON
    TestButton();
#endif
#ifdef TEST_EVENTS
    TestEvents();
#endif
#ifdef TEST_FLASH
    TestFlash();
#endif
#ifdef TEST_I2C
    TestI2C();
#endif
#ifdef TEST_LCD
    TestLCD();
#endif
#ifdef TEST_LWIP
    TestlwIP();
#endif
#ifdef TEST_PWM
    TestPWM();
#endif
#ifdef TEST_SD
    TestSD();
#endif
#ifdef TEST_TOUCHPANEL
    TestTouchPanel();
#endif
#ifdef TEST_USB
    TestUSB();
#endif
    while (true);
}
